package com.xckj.teacher.ui.activity

import com.blankj.utilcode.util.GsonUtils
import com.xckj.teacher.R
import com.xckj.teacher.databinding.ActivityClassDetailBinding
import com.xckj.teacher.framework.BaseActivity
import com.xckj.teacher.presenter.ClassPresenter
import com.xckj.teacher.ui.adapter.ClassItemAdapter
import com.xckj.teacher.ui.fragment.StudentDialog
import com.xckj.teacher.util.ActivityMessenger
import com.xckj.teacher.util.get


/**
 * 班级详情
 */
class ClassDetailActivity : BaseActivity<ActivityClassDetailBinding>() {
    private val presenter: ClassPresenter by lazy { ClassPresenter(this) }
    private val classItemAdapter: ClassItemAdapter by lazy {
        ClassItemAdapter(arrayListOf()) {
            StudentDialog(it) {

            }.show(this.supportFragmentManager, "")
        }
    }
    var id = 0

    inner class ProxyClick {
        fun back() {
            finish()
        }

        fun edit() {
            ActivityMessenger.startActivityForResult(
                this@ClassDetailActivity, EditClassActivity::class,
                "school" to "${dataBinding.data?.schoolname}",
                "class" to "${dataBinding.data?.name}",
                "grade" to "${dataBinding.data?.grade?.gradeName}",
                "studentNum" to "${dataBinding.data?.studeentNum}",
                "id" to "${dataBinding.data?.id}",
                "vipNum" to "${dataBinding.data?.vipstudent}"
            ) {
                if (it?.get<String>("refresh") == "1") {
                    ActivityMessenger.finish(this@ClassDetailActivity, "refresh" to "1")
                }
            }
        }

    }

    override fun loadLayout(): Int {
        return R.layout.activity_class_detail
    }

    override fun initData() {
        dataBinding.click = ProxyClick()
        id = intent.get<Int>("id") ?: 0
        dataBinding.rv.adapter = classItemAdapter
        presenter.classDetail(id.toString()) {
            dataBinding.data = it
            classItemAdapter.setList(it.group)
        }
    }

    override fun setListener() {
        classItemAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.edit -> {
                    ActivityMessenger.startActivityForResult(
                        this@ClassDetailActivity, GroupActivity::class,
                        "data" to GsonUtils.toJson(classItemAdapter.data[position]),
                        "classId" to "${dataBinding.data?.id}"
                    ) {
                        if (it != null) {
                            var get = it.get<String>("data")
                            if (get == "refresh") {
                                presenter.classDetail(id.toString()) {
                                    dataBinding.data = it
                                    classItemAdapter.setList(it.group)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}